package com.etheart.music.web.action.usersphere;

import java.util.Map;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.etheart.model.entity.User;
import com.etheart.music.web.action.usersphere.helper.UsersphereHelper;
import com.etheart.usersphere.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.inject.Inject;

@Controller("registerAction")
@Scope("prototype")
public class RegisterAction extends ActionSupport implements SessionAware,RequestAware ,ApplicationAware{
	
	private static final long serialVersionUID = 1L;
	
	private User user;
	@Inject("usersphere_UserService")
	private UserService userService;
	
	
	@Override
	public String execute() throws Exception {
		User u = userService.getUserByUsername(user.getUsername());
		if (u != null) {
			return "register";
		}
		
		userService.registerUser(user);
		UsersphereHelper.loginSession(session, user);
		return "message";
	}
	
	
	Map<String, Object> request;
	Map<String, Object> session;
	Map<String, Object> application;
	public void setRequest(Map<String, Object> arg0) {
		this.request = arg0;
	}
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}
	public void setApplication(Map<String, Object> arg0) {
		this.application=arg0;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	
}
